# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=epydoc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.1
pkgrel=1
pkgdesc="Tool for generating API documentation for Python modules, based on their docstrings (mingw-w64)"
arch=('any')
url="https://epydoc.sourceforge.net/"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
            "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-python2-docutils")
optdepends=("${MINGW_PACKAGE_PREFIX}-tk: needed for epydocgui"
            #"${MINGW_PACKAGE_PREFIX}-texlive-bin: needed for PDF conversion"
            "${MINGW_PACKAGE_PREFIX}-graphviz: needed for graph generation")
options=('strip' 'staticlibs')
source=("https://downloads.sourceforge.net/sourceforge/${_realname}/${_realname}-${pkgver}.zip"
        0001-handle-docutils-0.6.patch
        0002-python26-tokenizer.patch
        0003-string-exceptions.patch)
sha256sums=('574c1dca1b0e8783be0121c32f589cf56255cdb288b4d4e52e60f0a8bcf799cb'
            '84d6724e0fcb3a5307963cbe37875e72110bf707781c64e7ddff0dfe96eeb1ab'
            '8bfd54be68ee8e743ab470370042e40130e9cf8c0430d493401fa44eae2d66f6'
            '099a94ba394f0c1c4f3f721fc3c9cf982a54f182be457faa03a7bb54188c8364')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  # py2 fix
  sed -i "s|env python|&2|" `grep -Erl "env python" .`

  patch -p1 -i ${srcdir}/0001-handle-docutils-0.6.patch
  patch -p1 -i ${srcdir}/0002-python26-tokenizer.patch
  patch -p1 -i ${srcdir}/0003-string-exceptions.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf ${_realname}-${pkgver} build-${MINGW_CHOST}
  cd "${srcdir}/build-${MINGW_CHOST}"
  ${MINGW_PREFIX}/bin/python2 setup.py build
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  # fix python command in files
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${_f}
  done

  install -d "${pkgdir}${MINGW_PREFIX}/share/man/man1"
  install -m644 man/*.1 "${pkgdir}${MINGW_PREFIX}/share/man/man1"
  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
